package de.bensoft.jrail.business.service;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import de.bensoft.jrail.business.entity.Locomotive;

public class LocomotiveService {

	@Inject
	EntityManager em;
	
	@SuppressWarnings("unchecked")
	public List<Locomotive> getAllLocomotives() {
		Query q = em.createNamedQuery(Locomotive.GET_ALL);
		return q.getResultList();
	}
	
	public void deleteLocomotive(Locomotive l) {
		em.remove(l);
	}
	
	public Locomotive saveLocomotive(Locomotive l) {
		em.getTransaction().begin();

		try {
			if(l.getId() != null) {
				l = em.merge(l);
			} else {
				em.persist(l);
			}
			em.getTransaction().commit();
		} catch(Exception ex) {
			
			em.getTransaction().rollback();
		}
		
		return l;		
	}
}
